package com.enation.app.javashop.api.client;

import com.enation.app.javashop.model.distribution.dos.DistributionDO;
import com.enation.app.javashop.service.distribution.DistributionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * DistributionClient controller
 *
 * @author Chopper
 * @version v1.0
 * @since v7.0
 * 2018-08-14 下午1:54
 */
@RestController
@RequestMapping("/client/distribution")
@Validated
@ApiIgnore
public class DistributionClientController {

    @Autowired
    private DistributionManager distributionManager;

    /**
     * 新增分销商
     *
     * @param distributor
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public DistributionDO add(@RequestBody DistributionDO distributor) {
        return distributionManager.add(distributor);
    }

    /**
     * 更新Distributor信息
     *
     * @param distributor
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public DistributionDO edit(@RequestBody DistributionDO distributor) {
        return distributionManager.edit(distributor);
    }

    /**
     * 获取分销商
     *
     * @param memberId
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public DistributionDO getDistributorByMemberId(@RequestParam("member_id") Long memberId) {
        return distributionManager.getDistributorByMemberId(memberId);
    }

    /**
     * 根据会员id设置其上级分销商（两级）
     *
     * @param memberId 会员id
     * @param parentId 上级会员的id
     * @return 设置结果， trun=成功 false=失败
     */
    @RequestMapping(value = "/set", method = RequestMethod.POST)
    public boolean setParentDistributorId(@RequestParam("member_id") Long memberId, @RequestParam("parent_id") Long parentId) {
        return distributionManager.setParentDistributorId(memberId, parentId);
    }
}
